package com.example.demo.Controller;

import com.example.demo.Common.ResultData;
import com.example.demo.Entity.Customer;
import com.example.demo.Entity.Vendor;
import com.example.demo.Param.*;
import com.example.demo.Service.UserService;
import com.example.demo.Service.VendorService;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class VendorController {

    @Resource
    private VendorService vendorService;

    private static final Logger logger = LoggerFactory.getLogger(VendorController.class);

    @GetMapping("/vendor/getAll")
    public List<Vendor> getVendors(){
        return this.vendorService.getAll();
    }

    @PostMapping("/vendor/del")
    @ResponseBody
    public ResultData deleteVendor(@RequestBody String id) throws JSONException {
        JSONArray jsonArray = null;
        jsonArray = new JSONArray("["+id+"]");
        System.out.println(jsonArray.getJSONObject(0).get("id"));
        String realId = jsonArray.getJSONObject(0).get("id").toString();
        Long vid = Long.valueOf(realId);
        this.vendorService.deleteVendor(vid);
        return ResultData.success();
    }

    @PostMapping("/vendorAdd")
    @ResponseBody
    public ResultData addVendor(@RequestBody VendorInsertParam vendorInsertParam){
        //System.out.println(addressReq.getStreetName());
        //System.out.println(addressReq.getTel());
        // Address address = this.addressService.createOne(addressReq);
        Vendor vendor = this.vendorService.createOne(vendorInsertParam);
        // System.out.println(address.getStreetName());
        // System.out.println(address.getTel());
        return ResultData.success();
    }

    @PostMapping("/vendor/udate")
    @ResponseBody
    public ResultData uodateVendor(@RequestBody VendorUpdateParam vendorUpdateParam){
        //System.out.println(addressReq.getStreetName());
        //System.out.println(addressReq.getTel());
        // Address address = this.addressService.createOne(addressReq);
        this.vendorService.updateVendor(vendorUpdateParam);
        // System.out.println(address.getStreetName());
        // System.out.println(address.getTel());
        return ResultData.success();
    }

    @PostMapping("/vendor/search")
    @ResponseBody
    public List<Vendor> searchVendor(@RequestBody SearchVendorParam searchVendorParam){
        //System.out.println(addressReq.getStreetName());
        //System.out.println(addressReq.getTel());
        // Address address = this.addressService.createOne(addressReq);
        String vendorName = searchVendorParam.getVendorname();
        List<Vendor> vendors = new ArrayList<>();
        if(vendorName==null){
            vendors = this.vendorService.getAll();
            return vendors;
        }
        vendors = this.vendorService.searchVendor(vendorName);
        // System.out.println(address.getTel());
        return vendors;
    }

}
